package com.zui.entity.product;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;

import com.zui.entity.BaseAutoIdEntity;

@Entity
@Table(name="geo")
public class Geo extends BaseAutoIdEntity {
	private static final long serialVersionUID = 1L;
	
	/**
	 * ShenZhen
	 */
	public static long DEFAULT_CITY_ID = 268;
	
	private String geoCode;
	/**
	 * China id card prefix
	 * e.g.: ID card num in beijing is start with 110000.
	 */
	private String idCode;
	/**
	 * for 58.com City id
	 */
	private String id58;
	private String pid58;
	private String name;
	private String nameEn;
	/**
	 * PinYing name
	 */
	private String namePY;
	private GeoType geoType;
	private String secCode;
	private String abbr;
	
	private Set<GeoAssoc> geoAssocs = new LinkedHashSet<GeoAssoc>();
	
	//*********** utility methods *****************************
	
	/**
	 * Get Children Geos of this geo object.
	 * @return
	 */
	@Transient
	@JSON(serialize = false)
	public Set<Geo> getChildrenGeos() {
		Iterator<GeoAssoc> it = geoAssocs.iterator();
		Set<Geo> children = new LinkedHashSet<Geo>();
		while (it.hasNext())
			children.add(it.next().getGeoTo());
		return children;
	}
	
	/**
	 * get double select json data.
	 * @return
	 */
	@Transient
	@JSON(serialize = false)
	public String getDoubleSelectJson() {
		StringBuffer sb = new StringBuffer();
		
		Iterator<Geo> itCG = this.getChildrenGeos().iterator();
		while (itCG.hasNext()) {
			Geo geo = itCG.next();
			sb.append("\"" + geo.getName() + "\":" + "{");
			sb.append("\"key\":" + "\"" + geo.getId() + "\",");
			sb.append("\"values\": {" );
			
			Iterator<Geo> itCG2 = geo.getChildrenGeos().iterator();		
			while (itCG2.hasNext()) {
				Geo geo2 = itCG2.next();
				sb.append("\"" + geo2.getName() + "\":\"" + geo2.getId() + "\",");
			}
			if (sb.lastIndexOf(",") == sb.length()-1)
				sb.deleteCharAt(sb.lastIndexOf(","));
			sb.append("}" );
			sb.append("}," );
		}
		if (sb.lastIndexOf(",") == sb.length()-1)
			sb.deleteCharAt(sb.lastIndexOf(","));
		return sb.insert(0, "{").append("}").toString();
	}

	@Column(length=10)
	public String getIdCode() {	return idCode;}
	public String getName() {return name;}
	public String getNameEn() {return nameEn;}
	public String getNamePY() {return namePY;}
	
	@ManyToOne
	@JoinColumn(name="geoTypeId")
	public GeoType getGeoType() {	return geoType;	}
	
	@OneToMany(mappedBy = "geo", fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
	@OrderBy(value = "id")
	@JSON(serialize = false)
	public Set<GeoAssoc> getGeoAssocs() {return geoAssocs;	}
	
	public String getSecCode() {	return secCode;}
	public String getAbbr() {
		return abbr;
	}
	public String getGeoCode() {
		return geoCode;
	}
	public String getId58() {
		return id58;
	}
	public String getPid58() {
		return pid58;
	}	
	
	
	
	
	
	
	


	public void setGeoAssocs(Set<GeoAssoc> geoAssocs) {
		this.geoAssocs = geoAssocs;
	}
	public void setId58(String id58) {
		this.id58 = id58;
	}
	public void setPid58(String pid58) {
		this.pid58 = pid58;
	}
	public void setIdCode(String idCode) {
		this.idCode = idCode;
	}
	public void setNamePY(String namePY) {
		this.namePY = namePY;
	}

	public void setGeoCode(String geoCode) {
		this.geoCode = geoCode;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setNameEn(String nameEn) {
		this.nameEn = nameEn;
	}
	public void setGeoType(GeoType geoType) {
		this.geoType = geoType;
	}
	public void setSecCode(String secCode) {
		this.secCode = secCode;
	}
	public void setAbbr(String abbr) {
		this.abbr = abbr;
	}
	
}
